home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / BITSTR.H < prev    next >
C/C++ Source or Header  |  1990-04-12  |  6KB  |  172 lines

  1. /* -*-C-*-
  2.  
  3. $Header: bitstr.h,v 1.7 90/04/12 21:12:22 GMT jinx Exp $
  4.  
  5. Copyright (c) 1987, 1988, 1989, 1990 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. /* Bit string macros.  "Little indian" version. */
  36.  
  37. #define BIT_STRING_LENGTH_OFFSET    1
  38. #define BIT_STRING_FIRST_WORD        2
  39.  
  40. #define BIT_STRING_LENGTH_TO_GC_LENGTH(bits)                \
  41.   (((bits) + (OBJECT_LENGTH - 1)) / OBJECT_LENGTH)
  42.  
  43. #define LOW_MASK(nbits) ((1 << (nbits)) - 1)
  44. #define ANY_MASK(nbits, offset) ((LOW_MASK (nbits)) << (offset))
  45.  
  46. #define BIT_STRING_LENGTH(bit_string)                    \
  47.   ((long) (FAST_MEMORY_REF ((bit_string), BIT_STRING_LENGTH_OFFSET)))
  48.  
  49. #define BIT_STRING_MSW(bit_string)                    \
  50.   (BIT_STRING_WORD (BIT_STRING_HIGH_PTR (bit_string)))
  51.  
  52. #define BIT_STRING_LSW(bit_string)                    \
  53.   (BIT_STRING_WORD                            \
  54.    (MEMORY_LOC                                \
  55.     ((bit_string), (BIT_STRING_INDEX_TO_WORD ((bit_string), 0)))))
  56.  
  57. /* Byte order dependencies. */
  58.  
  59. #ifdef VAX_BYTE_ORDER
  60.  
  61. /*
  62.  
  63. Memory layout of bit strings:
  64.  
  65. +-------+-------+-------+-------+
  66. |  NMV    |  GC size (longwords)    | 0
  67. +-------+-------+-------+-------+
  68. |       Size in bits        | 1
  69. +-------+-------+-------+-------+
  70. |                 LSB| 2
  71. +-------+-------+-------+-------+
  72. |                | 3
  73. +-------+-------+-------+-------+
  74. .                . .
  75. .                . .
  76. .                . .
  77. +-------+-------+-------+-------+
  78. |MSB                    | N
  79. +-------+-------+-------+-------+
  80.  
  81. The last data word (marked as word "N" above) is where any excess
  82. bits are kept.
  83.  
  84. The "size in bits" is a C "long" integer.
  85.  
  86. */
  87.  
  88. #define BIT_STRING_HIGH_PTR(bit_string)                    \
  89.   (MEMORY_LOC ((bit_string), ((VECTOR_LENGTH (bit_string)) + 1)))
  90.  
  91. #define BIT_STRING_LOW_PTR(bit_string)                    \
  92.   (MEMORY_LOC ((bit_string), BIT_STRING_FIRST_WORD))
  93.  
  94. #define BIT_STRING_WORD(ptr)        (*((ptr) - 1))
  95. #define DEC_BIT_STRING_PTR(ptr)        (--(ptr))
  96. #define INC_BIT_STRING_PTR(ptr)        ((ptr)++)
  97.  
  98. /* This is off by one so BIT_STRING_WORD will get the correct word. */
  99.  
  100. #define BIT_STRING_INDEX_TO_WORD(bit_string, index)            \
  101.   ((BIT_STRING_FIRST_WORD + 1) + ((index) / OBJECT_LENGTH))
  102.  
  103. #define BIT_STRING_INDEX_PAIR_TO_INDEX(string, word, bit)        \
  104.   ((((word) - (BIT_STRING_FIRST_WORD + 1)) * OBJECT_LENGTH) + (bit))
  105.  
  106. #define READ_BITS_PTR(object, offset, end)                \
  107.   (MEMORY_LOC                                \
  108.    ((object), (BIT_STRING_LENGTH_TO_GC_LENGTH (((offset) + (end)) - 1))))
  109.  
  110. #define COMPUTE_READ_BITS_OFFSET(offset, end)                \
  111. {                                    \
  112.   offset = ((offset + end) % OBJECT_LENGTH);                \
  113.   if (offset != 0)                            \
  114.     offset = (OBJECT_LENGTH - offset);                    \
  115. }
  116.  
  117. #else /* not VAX_BYTE_ORDER */
  118.  
  119. /*
  120.  
  121. Memory layout of bit strings:
  122.  
  123. +-------+-------+-------+-------+
  124. |  NMV    |  GC size (longwords)    | 0
  125. +-------+-------+-------+-------+
  126. |       Size in bits        | 1
  127. +-------+-------+-------+-------+
  128. |MSB                | 2
  129. +-------+-------+-------+-------+
  130. |                | 3
  131. +-------+-------+-------+-------+
  132. .                . .
  133. .                . .
  134. .                . .
  135. +-------+-------+-------+-------+
  136. |                 LSB| N
  137. +-------+-------+-------+-------+
  138.  
  139. The first data word (marked as word "2" above) is where any excess
  140. bits are kept.
  141.  
  142. The "size in bits" is a C "long" integer.
  143. */
  144.  
  145. #define BIT_STRING_HIGH_PTR(bit_string)                    \
  146.   (MEMORY_LOC ((bit_string), BIT_STRING_FIRST_WORD))
  147.  
  148. #define BIT_STRING_LOW_PTR(bit_string)                    \
  149.   (MEMORY_LOC ((bit_string), ((VECTOR_LENGTH (bit_string)) + 1)))
  150.  
  151. #define BIT_STRING_WORD(ptr)        (*(ptr))
  152. #define DEC_BIT_STRING_PTR(ptr)        ((ptr)++)
  153. #define INC_BIT_STRING_PTR(ptr)        (--(ptr))
  154.  
  155. /* This is especially clever.  To understand it, note that the index
  156.    of the last pointer of a vector is also the GC length of the
  157.    vector, so that all we need do is subtract the zero-based word
  158.    index from the GC length. */
  159. #define BIT_STRING_INDEX_TO_WORD(bit_string, index)            \
  160.   ((VECTOR_LENGTH (bit_string)) - ((index) / OBJECT_LENGTH))
  161.  
  162. #define BIT_STRING_INDEX_PAIR_TO_INDEX(string, word, bit)        \
  163.   ((((VECTOR_LENGTH (string)) - (word)) * OBJECT_LENGTH) + (bit))
  164.  
  165. #define READ_BITS_PTR(object, offset, end)                \
  166.   (MEMORY_LOC ((object), ((offset) / OBJECT_LENGTH)))
  167.  
  168. #define COMPUTE_READ_BITS_OFFSET(offset, end)                \
  169.   (offset) = ((offset) % OBJECT_LENGTH);
  170.  
  171. #endif /* VAX_BYTE_ORDER */
  172.